package com.itheima.Service.Imp;

import com.itheima.Mapper.DeptMapper;
import com.itheima.Pojo.Dept;
import com.itheima.Service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImp implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    public List<Dept> list(){
        return deptMapper.selectAll();
    }

    /**
     * 根据id删除部门
     * @param id
     */
    @Override
    public void delete(Integer id) {
        deptMapper.delete(id);
    }

    /**
     * 新增部门
     * @param dept
     */
    @Override
    @Transactional
    public void addDept(Dept dept) {
        //1.补充属性
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    public void update(Dept dept) {
        //补充基本属性
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
